package com.leo.ibatis.demo.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.leo.ibatis.demo.criteria.LineitemCriteria;
import com.leo.ibatis.demo.domain.Lineitem;
import com.leo.ibatis.demo.service.LineitemService;

public class LineitemController extends MultiActionController {

	private LineitemService lineitemService;

	public void setLineitemService(LineitemService lineitemService) {
		this.lineitemService = lineitemService;
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		LineitemCriteria lineitemCriteria = new LineitemCriteria();
		bind(request, lineitemCriteria);
		
		List<Lineitem> lineitemList = lineitemService.listOnPage(lineitemCriteria);
		
		ModelAndView mav = new ModelAndView("lineitem/list");
		mav.addObject("lineitemList", lineitemList);
		mav.addObject("lineitemCriteria", lineitemCriteria);
		
		return mav;
	}

	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		LineitemCriteria lineitemCriteria = new LineitemCriteria();
		bind(request, lineitemCriteria);
			
		int linenum = ServletRequestUtils.getRequiredIntParameter(request, "linenum");
		int orderid = ServletRequestUtils.getRequiredIntParameter(request, "orderid");

		Lineitem lineitem = lineitemService.select(linenum, orderid);
		
		ModelAndView mav = new ModelAndView("lineitem/detail");
		mav.addObject("lineitem", lineitem);
		mav.addObject("lineitemCriteria", lineitemCriteria);
		
		return mav;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int linenum = ServletRequestUtils.getRequiredIntParameter(request, "linenum");
		int orderid = ServletRequestUtils.getRequiredIntParameter(request, "orderid");

		lineitemService.delete(linenum, orderid);
		
		return new ModelAndView("redirect:lineitem.do");
	}

}
